<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false" style="min-height: 500px">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <!--      <el-form-item label="优惠券id" prop="couponsId">-->
      <!--        <el-input v-model="dataForm.couponsId" placeholder="优惠券id"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="用户id" prop="userId">-->
      <!--        <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="用户名称" prop="username">-->
      <!--        <el-input v-model="dataForm.username" placeholder="用户名称"></el-input>-->
      <!--      </el-form-item>-->
      <el-form-item label="当前数量" prop="num"> 总量：{{ dataForm.num }} 已领取：{{ dataForm.received }} </el-form-item>
      <el-select v-model="dataForm.userIdNameList" style="width: 700px" filterable remote reserve-keyword multiple placeholder="输入手机号或姓名" :remote-method="getUserList" :loading="loading">
        <el-option v-for="item in userList" :key="item.value" :label="item.username + '-' + item.mobile" :value="item.id + ',' + item.username" :disabled="item.disabled"></el-option>
      </el-select>
      <!--      <el-form-item label="是否使用:0否，1是" prop="status">
              <el-input v-model="dataForm.status" placeholder="是否使用:0否，1是"></el-input>
            </el-form-item>
            <el-form-item label="订单id" prop="orderId">
              <el-input v-model="dataForm.orderId" placeholder="订单id"></el-input>
            </el-form-item>
            <el-form-item label="名称标题" prop="title">
              <el-input v-model="dataForm.title" placeholder="名称标题"></el-input>
            </el-form-item>
            <el-form-item label="开始时间" prop="startTime">
              <el-input v-model="dataForm.startTime" placeholder="开始时间"></el-input>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
              <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="createTime">
              <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
            </el-form-item>
            <el-form-item label="更新时间" prop="updateTime">
              <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
            </el-form-item>-->
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
import { IObject } from "@/types/interface";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const loading = ref(false);
const userList = ref<any[]>([]);

const dataForm = reactive({
  id: "",
  couponsId: "",
  // userId: "",
  // username: "",
  // status: "",
  // orderId: "",
  // title: "",
  // startTime: "",
  // endTime: "",
  // createTime: "",
  // updateTime: ""
  userIdNameList: [] as IObject[],
  num: 0,
  received: 0
});

const rules = ref({
  // couponsId: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // userId: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // username: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // status: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // orderId: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // title: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // startTime: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // endTime: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // createTime: [{required: true, message: "必填项不能为空", trigger: "blur"}],
  // updateTime: [{required: true, message: "必填项不能为空", trigger: "blur"}]
  userIdList: [{ required: true, message: "必填项不能为空", trigger: "blur" }]
});

const init = (id?: number, couponsId?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
  if (couponsId) {
    dataForm.couponsId = String(couponsId);
    baseService.get("/yckj/coupons/" + couponsId).then((res) => {
      if (res.data) {
        dataForm.num = res.data.num;
        dataForm.received = res.data.received;
      }
    });
  }
  getUserList("");
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/yckj/couponslogs/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 获取用户列表
const getUserList = async (query: string) => {
  loading.value = true;
  const res = await baseService.get("/yckj/couponslogs/user/list", { key: query, couponsId: dataForm.couponsId });
  userList.value = res.data;
  loading.value = false;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    if (dataForm.num - dataForm.received < dataForm.userIdNameList.length) {
      ElMessage.error({
        message: "优惠券数量不足",
        duration: 500
      });
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/yckj/couponslogs", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
